package eScrum.pages;

import org.apache.wicket.model.LoadableDetachableModel;

import eScrum.entities.User;
import eScrum.services.UserService;

public class DetachableUserModel  extends LoadableDetachableModel<User> {

	private static final long serialVersionUID = 1L;
	private final long id;

	private UserService userService;

	public DetachableUserModel(User u, UserService userService) {
		this(u.getId());
		this.userService = userService;
	}

	public DetachableUserModel(long id) {
		if (id == 0) {
			throw new IllegalArgumentException();
		}
		this.id = id;
	}

	@Override
	public int hashCode() {
		return Long.valueOf(id).hashCode();
	}

	@Override
	public boolean equals(final Object obj) {
		if (obj == this) {
			return true;
		} else if (obj == null) {
			return false;
		} else if (obj instanceof DetachableUserModel) {
			DetachableUserModel other = (DetachableUserModel) obj;
			return other.id == id;
		}
		return false;
	}

	@Override
	protected User load() {
		return userService.findById(id);
	}

}
